<?php
namespace Admin\Controller;

use Think\Controller;

class LoginController extends Controller
{
    public function index()
    {
        if (session('adminUser')) {
            $this->redirect('Index/index');
        }
        $this->display();
    }


    public function check()
    {
        $username = $_POST['username'];
        $password = $_POST['password'];
        $code = $_POST['code'];
        if (!trim($username)) {
            return show(0, "用户名不能为空");
        }
        if (!trim($password)) {
            return show(0, "密码不能为空");
        }
        if (!trim($code)) {
            return show(0, "验证码不能为空");
        }

        $ret = D('Admin')->getAdminByUsername($username);
        if (!$ret) {
            return show(0, '该用户不存在');
        }

        if ($ret['status'] != 1) {
            return show(0, '该用户被冻结');
        }

        if ($ret['password'] != getMd5Password($password)) {
            return show(0, '密码错误');
        }

        if (!check_verify($code)) {
            return show(0, '亲，验证码输错了哦！');
        }

        D("Admin")->updateByAdminId($ret['admin_id'], array('lastlogintime' => time()));
        session('adminUser', $ret);
        return show(1, '登录成功');
    }


    public function verify_c()
    {
        $Verify = new \Think\Verify();
        $Verify->entry();
    }

    public function loginout()
    {
        session('adminUser', null);
        $this->redirect('Login/index');

    }


}